package projorientado.ocr.vfeature.impl;

import projorientado.ocr.vfeature.*;
import projorientado.image.ImageComparator;
import projorientado.image.ImageLayerData;

/**
 *  Sistema de comparação entre duas imagens que usa os features extraidos para comparação.
 */
public class ImageFeaturesComparator implements ImageComparator {

    private FeaturesPairExtractor extractor;
    private FeaturesComparator comparator;
    private VisitorFeaturesComparator visitor;
    private int counter = 0;

    public ImageFeaturesComparator(FeaturesPairExtractor extractor, FeaturesComparator comparator) {
        this.extractor = extractor;
        this.comparator = comparator;
    }

    public ImageFeaturesComparator() {
    }

    public FeaturesComparator getComparator() {
        return comparator;
    }

    public void setComparator(FeaturesComparator comparator) {
        this.comparator = comparator;
    }

    public FeaturesPairExtractor getExtractor() {
        return extractor;
    }

    public void setExtractor(FeaturesPairExtractor extractor) {
        this.extractor = extractor;
    }
    
    public float compareImages(ImageLayerData image1, ImageLayerData image2) {
        FeaturesPair pair = extractor.extractFeaturesPair(image1, image2);
        if(!pair.isValid())
            System.out.println("Comparator error: invalid pair");
        float v = comparator.compareFeatures(pair.getFeatures1(),pair.getFeatures2());
        
        //v*=pair.getDiffFactor();
        
        if(visitor != null) {
            visitor.visit(counter++,pair.getFeatures1(),pair.getFeatures2(),v);
        }
        return v;
    }

    public VisitorFeaturesComparator getVisitor() {
        return visitor;
    }

    public void setVisitor(VisitorFeaturesComparator visitor) {
        this.visitor = visitor;
    }

}
